package com.hongxun.design.jungong.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.adapters.MultiSelectAdapter;
import com.hongxun.design.bean.UserBean;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.httputil.HttpStringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/11.添加项目参与人
 */

public class ProjectParticipantsDialog extends DialogFragment {
    private AlertDialog dialog;
    private Handler handler, handler0;
    public static View view;
    private MultiSelectAdapter multiAdapter;
    private List<UserBean> unitUsersData;
    private String userid = "";

    public ProjectParticipantsDialog() {
        unitUsersData = new ArrayList<>();
        handler0 = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        List<UserBean> unitsUser = FastJsonUtil.JsonPersonnelData((String) msg.obj, "12").get(0).getUnitsUser();
                        if (unitsUser == null) {
                            ToastShow.showShort(getActivity(), "没有查询到数据");
                        } else {
                            if (unitsUser.isEmpty()) ToastShow.showShort(getActivity(), "没有查询到数据");
                        }

                        unitUsersData.clear();
                        unitUsersData.addAll(unitsUser);
                        multiAdapter.setPz(unitUsersData);
                        multiAdapter.notifyDataSetChanged();
                        break;
                }
            }
        };
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        view = inflater.inflate(R.layout.propartiaipantsdialog, null);
        ViewHolder holder = new ViewHolder(view);
        holder.initData();
        dialog = builder.create();
        dialog.setView(view, 0, 0, 0, 0);
        return dialog;
    }

    class ViewHolder {
        TextView ppTvTitle;
        EditText ppEtName;
        TextView ppTvSearch;
        ListView ppLv;
        Button ppBtnNo;

        ViewHolder(View view) {
            view.setTag(this);
        }

        public void initData() {
            ppTvTitle = (TextView) view.findViewById(R.id.pp_tv_title);
            ppEtName = (EditText) view.findViewById(R.id.pp_et_name);
            ppTvSearch = (TextView) view.findViewById(R.id.pp_tv_search);
            ppLv = (ListView) view.findViewById(R.id.pp_lv);
            ppBtnNo = (Button) view.findViewById(R.id.pp_btn_no);

            SetTypeface.set(ppTvSearch, IconSourse.SEARCH);
            //搜索散户中心人员
            ppTvSearch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String valueOf = String.valueOf(ppEtName.getText());
                    MyApp.HTTP.getProParticipants(handler0, valueOf, HttpStringUtil.SUCCESSED);
                }
            });
            multiAdapter = new MultiSelectAdapter(unitUsersData, getActivity(), userid);
            ppLv.setAdapter(multiAdapter);
            ppLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    dialog.dismiss();
                    Message message = handler.obtainMessage();
                    message.obj = unitUsersData.get(position);
                    message.what = 2;
                    handler.sendMessage(message);
                }
            });
            ppBtnNo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
    }
}
